package com.huarun.module.applyrecord.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huarun.common.BaseController;
import com.huarun.common.CommonException;
import com.huarun.common.ResponseMessage;
import com.huarun.common.util.Constant;
import com.huarun.common.util.PageBean;
import com.huarun.common.util.ResultCodeEnum;
import com.huarun.module.applyrecord.bean.ApplyRecord;
import com.huarun.module.applyrecord.service.IApplyRecordService;
import com.huarun.module.cargorelease.bean.CargoReleaseApply;
import com.huarun.module.cargorelease.service.ICargoReleaseApplyService;
import com.huarun.module.user.bean.User;
import com.huarun.module.user.service.IUserService;

import io.swagger.annotations.ApiOperation;

/***
 * 
 * @author wyq
 * @创建时间 2018-12-12 16:13
 * @Description 申请记录
 */
@Controller
@RequestMapping("/wx/applyRecord")
public class ApplyRecordController extends BaseController {

	@Resource
	private IApplyRecordService applyRecordService;

	@Resource
	private IUserService userService;

	@Resource
	private ICargoReleaseApplyService cargoReleaseApplyService;

	@ApiOperation(value = "申请记录列表", notes = "申请记录列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<Object> saveApproval(HttpServletRequest req) throws Exception{
		ResponseMessage<Object> rm = new ResponseMessage<Object>();
		String type = req.getParameter("type");
//		String status = req.getParameter("status");
		String startDate = req.getParameter("startDate");
		String endDate = req.getParameter("endDate");
		String remark = req.getParameter("remark");
		String synId=req.getHeader("synId");
		String synMainId=req.getHeader("synMainId");
		String projectId=req.getHeader("projectId");
		int page=Integer.valueOf(req.getParameter("page"));
		int rows=Integer.valueOf(req.getParameter("rows"));
		Map<String, Object> params = new HashMap<String, Object>();
		if ("全部申请".equals(type)) {
			type = null;
		}
//		if ("全部".equals(status)) {
//			status = null;
//		}
		params.put("type", type);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("remark", remark);
//		params.put("status", status);
		params.put("page", page);
		params.put("rows", rows);
		String openId=req.getHeader("openId");    
		Map<String, Object> userParam=new HashMap<String, Object>();
		userParam.put("openId", openId);
		User u=userService.getUserByParams(userParam);
		if(null!=u ) {
			// 2019/05/07更正规则 如果是租户 那么 只能查看到当前租户下自己申请的记录
			if(u.getAdmin()==Constant.LESSEE_ROLE) {
				params.put("phone", u.getWxPhone());
				params.put("synMainId", synMainId);
			}else if(u.getAdmin()==Constant.MAIN_ADMIN_ROLE || u.getAdmin()==Constant.SUB_ADMIN_ROLE) {
				params.put("synMainId", synMainId);
				//如果是管理或子管理，那么只能查到自己这个租户下面的
			}else {
				//如果是访客，那么什么记录也查不到，因为type并无9，所以用9做查询
				params.put("type", "9");
			}
		}
		
		
		//如果子管理ID和主管理ID是同一个的话，那么这个人就是主管理员了，则可以查询这个项目下这个租户的所有记录（包括子管理）
		//如果ID不同，那么只能查看到这个项目下自己提交的记录了
		params.put("projectId", projectId);
		if(null==synId || null==synMainId) {
			rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, "管理员信息有误，请刷新操作");
			return rm;
		}
		//此处记录页面，同一租户下，管理员和子管理都可以看到相同的信息 update by eva  2019/2/22
//		if(synId.equals(synMainId)) {
//			params.put("synMainId", synMainId);
//		}
//			else {
//			params.put("synId", synId);
//		}
			
			
		
		try {
			PageBean<ApplyRecord> data = applyRecordService.findListPage(params);
			Map<String, Object> map = null;
			List<Map<String, Object>> list = new ArrayList<>();
			for (ApplyRecord info : data.getPages()) {
				map = new HashMap<>();
				map.put("id", info.getId());
				map.put("applyId", info.getApplyId());
				map.put("operateNo", info.getOperateNo());
				String operateStatus = applyRecordService.getOperateStatus(info.getOperateNo(), req);
				if (null == operateStatus) {
					//如果转换失败，默认设置为0
					CargoReleaseApply apply = cargoReleaseApplyService.getById(NumberUtils.toInt(info.getOperateNo(),0));
					
					if(null!=apply) {
						operateStatus=apply.getStatus();
					}
				}
				System.out.println("status======================"+operateStatus);
				map.put("status", operateStatus);
				map.put("userId", info.getUserId());
				map.put("phone", info.getPhone());
				map.put("typeCode", info.getTypeCode());
				map.put("applyType", info.getApplyType());
				map.put("title", info.getTitle());
				map.put("location", info.getLocation());
				map.put("description", info.getDescription());
				map.put("interviewee", info.getInterviewee());
				map.put("repairType", info.getRepairType());
				map.put("createDate", info.getCreateDate());
				map.put("updateDate", info.getUpdateDate());
//				map.put("status", info.getStatus());
				list.add(map);
			}
			PageBean<Map<String, Object>> newData = new PageBean<>();
			newData.setPages(list);
			newData.setCurrPage(data.getCurrPage());
			newData.setTotalCount(data.getTotalCount());
			newData.setPageSize(data.getPageSize());
			newData.setTotalPageCount(data.getTotalPageCount());
			rm.setData(newData);
			rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
		} catch (CommonException e) {
			rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
			log.error("查询申请记录列表异常！");
			e.printStackTrace();
		}
		return rm;
	}

}
